home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 57918 / 57918.xpi / chrome / gic2.jar / content / statusbar.js < prev    next >
Encoding:
Text File  |  2010-01-26  |  10.0 KB  |  410 lines

  1. var gic2 = {
  2.  
  3.     id: 2,
  4.     version: 1.1,
  5.     page: 'google-analytics-firefox-extension',
  6.     name: 'Google Analytics Watcher',
  7.  
  8.     prefs: null,
  9.     token: '',
  10.     profiles: '',
  11.     profile_last: 0,
  12.     profile_set_menu: false,
  13.  
  14.     startup: function()
  15.     {
  16.         // Name
  17.         this.gebi('label').label = this.name;
  18.  
  19.         // Preferences
  20.         this.prefs = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService).getBranch('extensions.gic'+this.id+'.');
  21.         this.prefs.QueryInterface(Components.interfaces.nsIPrefBranch2);
  22.         this.prefs.addObserver('', this, false);
  23.  
  24.         // Install & update
  25.         if (this.prefs.getIntPref('first'))
  26.         {
  27.             this.prefs.setIntPref('first', 0);
  28.             setTimeout(this.web, 500, '/'+this.page+'/?mode=welcome');
  29.         }
  30.         var pref_version = this.prefs.getCharPref('version');
  31.         if (pref_version < this.version)
  32.         {
  33.             this.prefs.setCharPref('version', this.version);
  34.             setTimeout(this.web, 500, '/'+this.page+'/?mode=update&from='+pref_version+'&to='+this.version);
  35.         }
  36.  
  37.         this.token    = this.prefs.getCharPref('token');
  38.         this.profiles = this.prefs.getCharPref('profiles');
  39.         this.profile_last = this.prefs.getIntPref('profile_last');
  40.  
  41.         this.profiles_menu();
  42.  
  43.         if (!this.cache()) this.main();
  44.     },
  45.  
  46.     auth: function()
  47.     {
  48.         gic2.prefs.setCharPref('token', '');
  49.  
  50.         var pref_email    = this.prefs.getCharPref('email');
  51.         var pref_password = this.prefs.getCharPref('password');
  52.  
  53.         if (!pref_email || !pref_password)
  54.         {
  55.             gic2.gebi('label').style.color = '#000';
  56.             return;
  57.         }
  58.  
  59.         var ajax = null;
  60.         ajax = new XMLHttpRequest();
  61.         ajax.open('POST', 'https://www.google.com/accounts/ClientLogin'+'?'+(new Date()).getTime(), true);
  62.         ajax.setRequestHeader('GData-Version', '2');
  63.         ajax.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  64.         ajax.onload = function()
  65.         {
  66.             gic2.gebi('label').style.color = '#000';
  67.  
  68.             if (ajax.status == 200)
  69.             {
  70.                 var regexp = /^Auth=.+$/im;
  71.                 var matches = ajax.responseText.match(regexp);
  72.                 if (!matches[0])
  73.                 {
  74.                     gic2.gebi('label').label = 'Google syntax error.';
  75.                 }
  76.                 else
  77.                 {
  78.                     gic2.token = matches[0];
  79.                     gic2.prefs.setCharPref('token', gic2.token);
  80.                 }
  81.             }
  82.             else
  83.             {
  84.                 gic2.gebi('label').label = 'Error #'+ajax.status+' '+ajax.responseText;
  85.             }
  86.         };
  87.         ajax.send('accountType=GOOGLE&Email='+pref_email+'&Passwd='+pref_password+'&service=analytics&source=gic-gic'+this.id+'-'+this.version);
  88.     },
  89.  
  90.     accounts: function()
  91.     {
  92.         if (!this.token) this.auth();
  93.         if (!this.token) return;
  94.  
  95.         this.prefs.setCharPref('profiles', '');
  96.  
  97.         var ajax = null;
  98.         ajax = new XMLHttpRequest();
  99.         ajax.open('GET', 'https://www.google.com/analytics/feeds/accounts/default?prettyprint=true'+'&'+(new Date()).getTime(), true);
  100.         ajax.setRequestHeader('Authorization', 'GoogleLogin '+this.token);
  101.         ajax.setRequestHeader('GData-Version', '2');
  102.         ajax.onload = function()
  103.         {
  104.             gic2.gebi('label').style.color = '#000';
  105.  
  106.             if (ajax.status == 200)
  107.             {
  108.                 var xml = ajax.responseXML;
  109.  
  110.                 try
  111.                 {
  112.                     var entry = xml.getElementsByTagName('entry');
  113.                     var profiles = [];
  114.                     for (var i=0; i<entry.length; i++)
  115.                     {
  116.                         profiles[i] = entry[i].getElementsByTagName('title').item(0).firstChild.data+'|'+entry[i].getElementsByTagName('dxp:tableId').item(0).firstChild.data;
  117.                     }
  118.                     gic2.profiles = profiles.join('||');
  119.                 }
  120.                 catch(e) {}
  121.  
  122.                 if (gic2.profiles)
  123.                 {
  124.                     gic2.prefs.setCharPref('profiles', gic2.profiles);
  125.                 }
  126.                 else
  127.                 {
  128.                     gic2.gebi('label').label = 'Error: there are no profiles in your account.';
  129.                 }
  130.             }
  131.             else
  132.             {
  133.                 // Invalid token
  134.                 if (ajax.status == 401)
  135.                 {
  136.                     gic2.auth();
  137.                     gic2.accounts();
  138.                 }
  139.                 else
  140.                 {
  141.                     gic2.gebi('label').label = 'Error #'+ajax.status;
  142.                 }
  143.             }
  144.         };
  145.         ajax.send(null);
  146.     },
  147.  
  148.     main: function()
  149.     {
  150.         this.gebi('label').style.color = '#FFF';
  151.  
  152.         if (!this.token) this.auth();
  153.         if (!this.token) return;
  154.  
  155.         if (!this.profiles) this.accounts();
  156.         if (!this.profiles) return;
  157.  
  158.         // Direct click
  159.         if (this.profile_set_menu)
  160.         {
  161.             this.profile_set_menu = false;
  162.         }
  163.         else
  164.         {
  165.             // Switch profiles on click
  166.             if (this.prefs.getBoolPref('profile_switch_click'))
  167.             {
  168.                 this.profile_last++;
  169.                 this.prefs.setIntPref('profile_last', this.profile_last);
  170.             }
  171.         }
  172.  
  173.         var profiles = this.profiles.split('||');
  174.  
  175.         // In array
  176.         if (this.profile_last > (profiles.length - 1))
  177.         {
  178.             this.profile_last = 0;
  179.             this.prefs.setIntPref('profile_last', this.profile_last);
  180.         }
  181.         // Current profile
  182.         var profile = profiles[this.profile_last].split('|');
  183.  
  184.         // Profiles in menu
  185.         this.profiles_menu();
  186.  
  187.         var date = new Date();
  188.         var date_today = date.toLocaleFormat('%Y-%m-%d');
  189.  
  190.         var ajax = null;
  191.         ajax = new XMLHttpRequest();
  192.         ajax.open('GET', 'https://www.google.com/analytics/feeds/data?ids='+profile[1]+'&metrics=ga:bounces,ga:newVisits,ga:pageviews,ga:visitors,ga:visits&start-date='+date_today+'&end-date='+date_today+'&prettyprint=true'+'&'+(new Date()).getTime(), true);
  193.         ajax.setRequestHeader('Authorization', 'GoogleLogin '+this.token);
  194.         ajax.setRequestHeader('GData-Version', '2');
  195.         ajax.onload = function()
  196.         {
  197.             gic2.gebi('label').style.color = '#000';
  198.  
  199.             if (ajax.status == 200)
  200.             {
  201.                 var xml = ajax.responseXML;
  202.  
  203.                 var label = gic2.tpl(xml, 'label');
  204.                 var tooltip = gic2.tpl(xml, 'tooltip');
  205.  
  206.                 // Cache
  207.                 gic2.prefs.setCharPref('cache_label', label);
  208.                 gic2.prefs.setCharPref('cache_tooltip', tooltip);
  209.                 gic2.prefs.setCharPref('cache_update', (new Date()).getTime());
  210.  
  211.                 gic2.gebi('label').label = label;
  212.                 gic2.gebi('tooltip').getElementsByTagName('description')[0].value = tooltip;
  213.  
  214.                 // Copy & tooltip
  215.                 gic2.gebi('menu_copy_label').label = 'Copy value "' + label + '"';
  216.                 gic2.gebi('menu_copy_tooltip').label = 'Copy title "' + tooltip + '"';
  217.                 gic2.gebi('menu_copy_label').value = label;
  218.                 gic2.gebi('menu_copy_tooltip').value = tooltip;
  219.                 if (tooltip)
  220.                 {
  221.                     gic2.gebi('menu_copy_tooltip').hidden = false;
  222.                     gic2.gebi('tooltip').hidden = false;
  223.                 }
  224.             }
  225.             else
  226.             {
  227.                 // Invalid token
  228.                 if (ajax.status == 401)
  229.                 {
  230.                     gic2.auth();
  231.                     gic2.main();
  232.                 }
  233.                 // Invalid profile
  234.                 else if (ajax.status == 400)
  235.                 {
  236.                     gic2.accounts();
  237.                     gic2.main();
  238.                 }
  239.                 else
  240.                 {
  241.                     gic2.gebi('label').label = 'Error #'+ajax.status;
  242.                 }
  243.             }
  244.         };
  245.         ajax.send(null);
  246.     },
  247.  
  248.     profiles_menu: function()
  249.     {
  250.         if (!this.profiles) return;
  251.  
  252.         var profiles = this.profiles.split('||');
  253.  
  254.         // Remove
  255.         var profiles_menu = this.gebi('profiles');
  256.         while(profiles_menu.hasChildNodes())
  257.         {
  258.             profiles_menu.removeChild(profiles_menu.firstChild);
  259.         }
  260.  
  261.         // Create
  262.         const XUL_NS = 'http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul';
  263.  
  264.         profiles_menu.appendChild(document.createElementNS(XUL_NS, 'menuseparator'));
  265.  
  266.         var menuitem = null;
  267.         var profile = [];
  268.         for (var i=0; i<profiles.length; i++)
  269.         {
  270.             profile = profiles[i].split('|');
  271.             menuitem = document.createElementNS(XUL_NS, 'menuitem');
  272.             menuitem.setAttribute('label', profile[0]);
  273.             menuitem.setAttribute('name', 'gic2_profile');
  274.             menuitem.setAttribute('type', 'radio');
  275.             menuitem.setAttribute('oncommand', 'gic2.profile_set('+i+');');
  276.             if (i == this.profile_last) menuitem.setAttribute('checked', 'true');
  277.  
  278.             profiles_menu.appendChild(menuitem);
  279.         }
  280.  
  281.         this.gebi('profiles').hidden = false;
  282.     },
  283.  
  284.     profile_set: function(profile_last)
  285.     {
  286.         this.profile_set_menu = true;
  287.         this.profile_last = profile_last;
  288.         this.main();
  289.     },
  290.  
  291.     profiles_reload: function()
  292.     {
  293.         this.prefs.setCharPref('profiles', '');
  294.         this.profiles = '';
  295.         this.profile_last = 0;
  296.         this.main();
  297.     },
  298.  
  299.     shutdown: function()
  300.     {
  301.         this.prefs.removeObserver('', this);
  302.     },
  303.  
  304.     observe: function(subject, topic, data)
  305.     {
  306.         if (topic != 'nsPref:changed') return;
  307.  
  308.         switch(data)
  309.         {
  310.             case 'email':
  311.                 this.token = '';
  312.                 this.profiles = '';
  313.                 this.profile_last = 0;
  314.  
  315.                 this.prefs.setCharPref('token', '');
  316.                 this.prefs.setCharPref('profiles', '');
  317.                 this.prefs.setIntPref('profile_last', 0);
  318.  
  319.                 this.main();
  320.             break;
  321.         }
  322.     },
  323.  
  324.  
  325.  
  326.     main_click: function(event)
  327.     {
  328.         // Disable right click
  329.         if (event.button != 2) this.main();
  330.     },
  331.  
  332.     cache: function()
  333.     {
  334.         var cache_label = this.prefs.getCharPref('cache_label');
  335.         var cache_tooltip = this.prefs.getCharPref('cache_tooltip');
  336.  
  337.         if (!cache_label) return false;
  338.  
  339.         if (((new Date()).getTime() - this.prefs.getCharPref('cache_update')) > (this.prefs.getIntPref('cache_interval') * this.prefs.getIntPref('cache_unit') * 1000)) return false;
  340.  
  341.         this.gebi('label').label = cache_label;
  342.         this.gebi('tooltip').getElementsByTagName('description')[0].value = cache_tooltip;
  343.  
  344.         return true;
  345.     },
  346.  
  347.     tpl: function(xml, tpl_name)
  348.     {
  349.         var tpl = this.prefs.getCharPref(tpl_name);
  350.         if (!tpl) return '';
  351.  
  352.         var regexp = /<([a-z0-9_:,-]+)>/ig;
  353.         return tpl.replace(regexp, function(str, match)
  354.         {
  355.             if (!match) return;
  356.  
  357.             var tpl_parts = match.split(',');
  358.             var tpl_parts_c = tpl_parts.length;
  359.  
  360.             if (tpl_parts_c > 3) return '<'+match+'>';
  361.  
  362.             // Clean
  363.             if (!(/^[0-9]+$/.test(tpl_parts[tpl_parts_c-1])))
  364.             {
  365.                 if (tpl_parts_c == 3) return '<'+match+'>';
  366.                 else
  367.                 {
  368.                     tpl_parts.push(1);
  369.                     tpl_parts_c++;
  370.                 }
  371.             }
  372.  
  373.             var value = '';
  374.             try
  375.             {
  376.                 if (tpl_parts_c == 3)
  377.                 {
  378.                     value = xml.getElementsByTagName(tpl_parts[0]).item(tpl_parts[2]-1).getAttribute(tpl_parts[1]);
  379.                 }
  380.                 else
  381.                 {
  382.                     value = xml.getElementsByTagName(tpl_parts[0]).item(tpl_parts[1]-1).firstChild.data;
  383.                 }
  384.             }
  385.             catch(e) {}
  386.  
  387.             if (value) return value;
  388.             else return '<'+match+'>';
  389.         });
  390.     },
  391.     dialog: function(name)
  392.     {
  393.         window.openDialog('chrome://gic'+this.id+'/content/'+name+'.xul', 'gic'+this.id+'_'+name, 'chrome, centerscreen, modal');
  394.     },
  395.     copy: function(str)
  396.     {
  397.         Components.classes["@mozilla.org/widget/clipboardhelper;1"].getService(Components.interfaces.nsIClipboardHelper).copyString(str);
  398.     },
  399.     gebi: function(name)
  400.     {
  401.         return document.getElementById('gic'+this.id+'_'+name);
  402.     },
  403.     web: function(uri)
  404.     {
  405.         gBrowser.selectedTab = gBrowser.addTab('http://getinformer.com'+uri);
  406.     }
  407. }
  408.  
  409. window.addEventListener('pageshow', function(e) {gic2.startup();}, false);
  410. window.addEventListener('unload', function(e) {gic2.shutdown();}, false);